Visualize the output

Code
import sys
sys.path.append("/home/yxiao/TMA22_Sep8_Skeleton")
Code
G_all_path = '/home/yxiao/TMA22_Sep8_Skeleton/output/OVTMA_fov216/graphs/G_all_gabriel.pkl'
with open(G_all_path, 'rb') as f:
    G_all = pickle.load(f)
df = pd.read_csv("/home/yxiao/TMA22_Sep8_Skeleton/output/OVTMA_fov216/data/df.csv")
df_aligned = align_df_with_G_all(df, G_all)

cmap = {'meta_2':'red','meta_3':'blue'}

plot cells

Code
import pandas as pd
import matplotlib.pyplot as plt


def plot_cells(df: pd.DataFrame, ax=None, s=4, alpha=0.8):
    if ax is None:
        fig, ax = plt.subplots()
    for ct, d in df.groupby("phenotype"):
        color = cmap.get(ct, "lightgray")
        ax.scatter(d["x"], d["y"], s=s, alpha=alpha, label=str(ct), color=color)

    #ax.set_aspect("equal")
    #ax.legend(title="Phenotype", fontsize=8)
    #ax.set_xlabel("x"); ax.set_ylabel("y")
    return ax
# divide df into each ROI, and plot the cells in each ROI in grid
n_roi = len(df['ROI'].unique())
n_cols = 5
n_rows = n_roi // n_cols + 1
fig, axs = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(5*n_cols, 5*n_rows))
for i, roi in enumerate(df['ROI'].unique()):
    ax = axs[i // n_cols, i % n_cols]
    plot_cells(df[df['ROI'] == roi], ax=ax,s=.25)
    ax.set_title(roi)
plt.show()

plot graphs

Code


cmap = {'meta_2': 'red', 'meta_3': 'blue'}
default_color = 'lightgray'
roi_col, id_col = "ROI", "cell_id"     # adjust if different
x_col, y_col   = "x", "y"              # adjust if different

# collect all ROI names present in G_all
all_rois = sorted({roi for (roi, _) in G_all.nodes()})

N = 20
for roi in islice(all_rois, N):
    # nodes for this ROI
    nodes_roi = [n for n in G_all.nodes() if n[0] == roi]
    if not nodes_roi:
        continue
    G_sub = G_all.subgraph(nodes_roi).copy()

    # align df to subgraph node order
    df_aligned = align_df_with_G_all(df, G_sub)

    # build positions dict from aligned df (expects x/y columns in df)
    pos = {node: (row[x_col], row[y_col]) for node, row in df_aligned.iterrows()}

    # node colors from phenotype
    pheno = df_aligned["phenotype"].astype(str)
    node_colors = [cmap.get(p, default_color) for p in pheno.values]

    # plot
    print(roi)
    fig, ax = plt.subplots(figsize=(6, 6))
    nx.draw(
        G_sub,
        pos=pos,
        ax=ax,
        node_size=6,
        node_color=node_colors,
        edge_color="#cccccc",
        width=0.5,
        alpha=0.9,
        with_labels=False,
    )
    ax.set_title(f"ROI: {roi}")
    ax.set_aspect("equal", adjustable="box")
    ax.axis("off")
    plt.show()
    plt.close(fig)
region_002

region_004

region_006

region_007

region_009

region_012

region_013

region_014

region_015

region_016

region_019

region_020

region_021

region_022

region_025

region_026

region_027

region_028

region_029

region_030

Code
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
import pickle
from itertools import islice

def plot_graph(G: nx.Graph, df: pd.DataFrame, ax=None, node_size=6, alpha=0.7):
    if ax is None:
        fig, ax = plt.subplots()
    pos = {i:(row.x, row.y) for i, row in df[["x","y"]].iterrows()}
    nx.draw_networkx_edges(G, pos=pos, ax=ax, width=0.5, alpha=alpha)
    nx.draw_networkx_nodes(G, pos=pos, ax=ax, node_size=node_size, alpha=alpha)
    ax.set_aspect("equal")
    ax.set_xlabel("x"); ax.set_ylabel("y")
    return ax

with open("../output/OVTMA_fov216/graphs/graph_dict_gabriel.pkl", "rb") as f:
    graph_dict = pickle.load(f)

for roi, G in islice(graph_dict.items(), 10):
    print(roi)
    plot_graph(G, df, ax=None, node_size=6, alpha=0.7)
    plt.show()
region_002

region_004

region_006

region_007

region_009

region_012

region_013

region_014

region_015

region_016

plot umaps with embeddings

for each A^k, see whether the embeddings show separation pattern

Code

def align_df_with_G_all(df, G_all):
    roi_col = "ROI"
    id_col  = "cell_id"  
    # 1) Build the target index from the graph's nodes (keeps the graph's order)
    node_index = pd.MultiIndex.from_tuples(list(G_all.nodes()),
                                        names=[roi_col, id_col])
    # 2) Set a matching MultiIndex on df
    #    (fails loudly if duplicates — better than silently picking a row)
    df_idxed = df.set_index([roi_col, id_col])
    if df_idxed.index.has_duplicates:
        print("Duplicates found in df")
        # pick the first occurrence per (ROI, cell_id); or use a different reducer
        df_idxed = df_idxed[~df_idxed.index.duplicated(keep="first")]

    # 3) Reindex to align order exactly to G_all.nodes()
    df_aligned = df_idxed.reindex(node_index)
    return df_aligned
Code
import scipy.io
import matplotlib.pyplot as plt
import pandas as pd
#from src.utils import align_df_with_G_all

# reorder df according to G_all.nodes() order
G_all_path = '/home/yxiao/TMA22_Sep8_Skeleton/output/OVTMA_fov216/graphs/G_all_gabriel.pkl'
with open(G_all_path, 'rb') as f:
    G_all = pickle.load(f)
df = pd.read_csv("/home/yxiao/TMA22_Sep8_Skeleton/output/OVTMA_fov216/data/df.csv")
df_aligned = align_df_with_G_all(df, G_all)

for i in range(1,11):   
    umap_embedding_path = f'/home/yxiao/TMA22_Sep8_Skeleton/output/OVTMA_fov216/basis_embedding/alpha_-0.5/umap/basis_{i}.mat'
    umap_embedding = scipy.io.loadmat(umap_embedding_path)["U"]
    #df = pd.read_csv("../output/OVTMA_fov216/data/df.csv")

    cmap = {'meta_2':'red','meta_3':'blue'}
    plt.scatter(umap_embedding[:,0], umap_embedding[:,1], c=df_aligned["phenotype"].map(cmap))
    plt.show()